<template>
    <div class="content">
        <uni-easyinput v-model="msg" type="textarea" auto-height placeholder="请输入内容"></uni-easyinput>
        <button @click="onSendClick">发送消息</button>
        <div class="box">
            <div v-for="item in messages" :key="item.host" class="item">服务器[{{ item.host }}]回复消息：{{ item.msg }}</div>
        </div>
    </div>
</template>

<script>
import { UDPClient } from '@/uni_modules/uts-udp';
import Vue from 'vue';

export default Vue.extend({
    data() {
        return {
            msg: '',
            messages: []
        };
    },
    methods: {
        onSendClick() {
            UDPClient.send({
                host: '255.255.255.255',
                port: 7000,
                receiveTimeout: 2000,
                msg: this.msg,
                enableRecive: false,
                onceReceive: (data) => {
                    this.messages.push(data);
                },
                onError: (error) => {
                    console.error(error);
                },
                onceReceiveTimeout: () => {
                    console.warn('服务器超时未回复');
                },
            });
        }
    }
});
</script>

<style lang="scss" scoped>
.content {
    font-size: 15px;
}

.box {
    padding: 10px;
}
</style>
